//
//  ZM_TextView_TVCell.m
//  YYFMMall
//
//  Created by chenzm on 2019/9/11.
//  Copyright © 2019 YuYou1. All rights reserved.
//

#import "ZM_TextView_TVCell.h"

@interface ZM_TextView_TVCell()

@end

@implementation ZM_TextView_TVCell{
    NSInteger _limit;/// 限制字数
}

#pragma mark - 赋值
-(void)setModel:(BaseM *)model{
    _model = model;
    self.textV.text = kIsEmptyStr(model.bsTitle1)?@"":model.bsTitle1;
    self.placeHolderLbl.text = kIsEmptyStr(model.bsPlaceHolder1)?@"":model.bsPlaceHolder1;
    self.placeHolderLbl.hidden = model.bsTitle1.length;
    
    _limit = model.bsCount == 0?1000:model.bsCount;
    self.wordsNumLbl.hidden = model.bsCount == 0?YES:NO;
    self.wordsNumLbl.text = [NSString stringWithFormat:@"%ld/%ld",(long)self.textV.text.length,(long)_limit];
}

-(void)setMaxWordNum:(NSInteger)maxWordNum{
    _maxWordNum = maxWordNum;
    _limit = maxWordNum == 0?1000:maxWordNum;
    self.wordsNumLbl.hidden = maxWordNum == 0?YES:NO;
    self.wordsNumLbl.text = [NSString stringWithFormat:@"%ld/%ld",(long)self.textV.text.length,(long)_limit];
}

#pragma mark - Methods
- (void)textViewDidChange:(UITextView *)textView {
    if (textView.text.length > _limit) {
        textView.text = [textView.text substringToIndex:_limit];
    }
    if (textView.text.length) {
        self.placeHolderLbl.hidden = YES;
    } else {
        self.placeHolderLbl.hidden = NO;
    }
    self.wordsNumLbl.text = [NSString stringWithFormat:@"%ld/%ld",(long)textView.text.length,(long)_limit];
    !self.txtDidChangeBlock?:self.txtDidChangeBlock(textView.text);
}

- (void)textViewDidEndEditing:(UITextView *)textView{
    !self.txtDidEndBlock?:self.txtDidEndBlock(textView.text);
}


#pragma mark - Intial
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUpBaseData];
        [self setUpUI];
    }
    return self;
}

///基本数据设置
-(void)setUpBaseData{
    _limit = 30;
}
///添加控件
-(void)setUpUI{
    [self textV];
    [self placeHolderLbl];
    [self wordsNumLbl];
}

#pragma mark - 布局
-(void)layoutSubviews{
    [super layoutSubviews];
    
}


#pragma mark - lazyLoad
-(UITextView *)textV{
    if (!_textV) {
        _textV = [UITextView new];
        _textV.delegate = self;
        _textV.font = kMediumFont(14);
        [self addSubview:_textV];
        [_textV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(8);
            make.bottom.mas_equalTo(-8);
            make.left.mas_equalTo(16);
            make.right.mas_equalTo(-16);
        }];
        
    }
    return _textV;
}

-(UILabel *)placeHolderLbl{
    if (!_placeHolderLbl) {
        _placeHolderLbl = [UILabel new];
        _placeHolderLbl.font = kMediumFont(14);
        _placeHolderLbl.textColor = kHexColor(0xD2D2D2);
        _placeHolderLbl.textAlignment = NSTextAlignmentLeft;
        _placeHolderLbl.text = @"请输入";
        [self addSubview:_placeHolderLbl];
        [_placeHolderLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.textV).offset(8);
            make.left.mas_equalTo(self.textV).offset(8);
        }];
    }
    return _placeHolderLbl;
}

-(UILabel *)wordsNumLbl{
    if (!_wordsNumLbl) {
        _wordsNumLbl = [UILabel new];
        _wordsNumLbl.font = kMediumFont(12);
        _wordsNumLbl.textColor = kHexColor(0xD2D2D2);
        _wordsNumLbl.textAlignment = NSTextAlignmentLeft;
        _wordsNumLbl.text = @"(0/30)";
        [self addSubview:_wordsNumLbl];
        [_wordsNumLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.textV).offset(-10);
            make.bottom.mas_equalTo(self.textV).offset(-8);
        }];
    }
    return _wordsNumLbl;
}


@end

